﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using CCcteg.Common;
using Ccteg.Common;
using Ccteg.DB;


using LinqKit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Cctegaq.Api.Controllers.Api
{

    /// <summary>
    /// 安全目标管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TARController : ControllerBase
    {

        private readonly MySqlDbContext _dbContext;
        private readonly IMapper _mapper;

        /// <summary>
        /// 
        /// </summary>

        /// <param name="dbContext"></param>
        /// <param name="mapper"></param>
        public TARController(MySqlDbContext dbContext, IMapper mapper)
        {

            _dbContext = dbContext;
            _mapper = mapper;
        }
        /// <summary>
        /// 安全目标管理 理添加,修改 添加测试数据用
        /// </summary>
        /// <param name="model">model.id大于0修改,新增传0</param>
        /// <returns></returns>
        [HttpPost]
        [ModelValidationAttribute]
        public ResponseModel TARTARAddOrUpdate([FromBody] Tar_tar_ViewModel model)
        {
            var response = ResponseModelFactory.CreateInstance;
            try
            {
                using (_dbContext)
                {
                    var entity = _mapper.Map<Tar_tar_ViewModel,tar_tar>(model);
                    var entryEntity = _dbContext.Tar_Tar.Attach(entity);

                    if (entity.ID > 0)
                    {
                        
                        entryEntity.State = EntityState.Modified;
                        entryEntity.Property(p => p.CreatedOn).IsModified = false;
                        entryEntity.Property(p => p.CreatedBy).IsModified = false;
                        entryEntity.Property(p => p.Deleted).IsModified = false;
                        entity.ModifiedBy = "系统";
                        entity.ModifiedOn = System.DateTime.Now;

                    }
                    else
                    {
                        
                        entryEntity.Property(p => p.ModifiedBy).IsModified = false;
                        entryEntity.Property(p => p.ModifiedOn).IsModified = false;
                        entity.Deleted = false;
                        entity.CreatedBy = "系统";
                        entity.CreatedOn = System.DateTime.Now;
                    }
                    _dbContext.SaveChanges();
                    response.SetSuccess("操作成功");
                }

            }
            catch (Exception ex)
            {
                response.SetFailed("操作失败");
                response.Data = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 安全目标查询
        /// </summary>

        /// <param name="companyno">单位名称</param>
        /// <param name="year">年度</param>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">显示条数</param>
        /// <returns></returns>
        [HttpGet]
        public ResponseResultModel<IEnumerable<Tar_tar_ViewModel>> TARTARSearch(string companyno, string year, int pageindex = 1, int pagesize = 10)
        {
            using (_dbContext)
            {

                var response = ResponseModelFactory<IEnumerable<Tar_tar_ViewModel>>.CreateResultInstance;
                Expression<Func<tar_tar, bool>> where = p => true;

                //根据法律法规过滤
                
                where = where.CheckNullAnd(x => x.CompanyNo == companyno, companyno)
                    .CheckNullAnd(x => x.Year == year, year);

                
                var query = _dbContext.Tar_Tar.AsQueryable();

                query = query.Where(where.Expand()).OrderByDescending(x => x.ID);

                var list = query.Paged(pageindex, pagesize).ToList();
                var totalCount = query.Count();
                var data = list.Select(_mapper.Map<tar_tar, Tar_tar_ViewModel>);
                response.SetData(data, totalCount);
                return response;
            }

        }
    }
}
